package cn.HL.pet.controller;


import cn.HL.org.domain.Employee;
import cn.HL.org.service.IEmployeeService;
import cn.HL.pet.dto.AcceptOrderDto;
import cn.HL.pet.service.ISearchMasterMsgService;
import cn.HL.pet.domain.SearchMasterMsg;
import cn.HL.pet.query.SearchMasterMsgQuery;
import cn.HL.basic.util.AjaxResult;
import cn.HL.basic.util.PageList;
import cn.HL.system.listener.LoginContext;
import cn.HL.user.domain.Logininfo;
import cn.HL.user.domain.User;
import cn.HL.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IUserService userService;

    /**
     * 发布宠物消息
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request){
        try {
            Logininfo loginUser = LoginContext.getLoginUser(request);
            searchMasterMsgService.publish(msg,loginUser);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }
    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     *待审核
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query){
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 待处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toBeProcessed")
    public PageList<SearchMasterMsg> toBeProcessed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);
        Logininfo loginUser = LoginContext.getLoginUser(request);
        Employee employee = employeeService.loadShopByLogininfo(loginUser.getId());
        query.setShopId(employee.getShop_id());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 寻主池
     */
    @PostMapping("/findTheLord")
    public PageList<SearchMasterMsg> findTheLord(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }
    /**
     * 已完成
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }
    /**
     * user查看自己的列表
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Logininfo loginUser = LoginContext.getLoginUser(request);
        User user = userService.loadByLogininfoId(loginUser.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryPage(query);
    }



    /**
    * 接收订单
    */
    @PostMapping("/accept")
    public  AjaxResult acceptOrder(@RequestBody AcceptOrderDto dto ){
        try {
            searchMasterMsgService.acceptOrder(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }






    /**
     * 拒绝订单
     */
    @PostMapping("/reject/{id}")
    public  AjaxResult reject(@PathVariable("id")Long id ){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }






}
